home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_stones.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  6.3 KB  |  195 lines

  1. /* SHADER INFO 
  2.  *
  3.  * RudyCstones.sl
  4.  * 
  5.  * Copyright (C) 2002, Rudy Cortes   rcortes@hntb.com
  6.  * created  06/12/2002
  7.  * 
  8.  * This software is placed in the public domain and is provided as is 
  9.  * without express or implied warranty.
  10.  *
  11.  * Shader that creates a surface covered with stones of different sizes,
  12.  * trying to replicate the shader usr for the ground in "A BUGS LIFE".
  13.  * Uses st to create the rocks and "shader" space to create the grunge.
  14.  *
  15.  * Feel free to use this shader to create skin for any character, anywhere and
  16.  * everywhere, Just list me on the credits under "Shading Team" if you use the
  17.  *  hader as is, or under "shader info" if you do any minor modifications to
  18.  * this code.
  19.  ********************************************
  20.  *
  21.  * ka, Kd, Ks, roughness = the usual
  22.  * Km = amount of displacement
  23.  * displace = should the surface be bumped(0) or displaced (1)?
  24.  * minfreq & maxfreq = limits to the rock loop excecution
  25.  * grungefreq, grunge_Pow, grunginess = freqeuncy, power and depth of grunge
  26.  * stonecolor, groundcolor = this is obvious, isn't it?
  27.  * varyhue, varysat,varylum  = how much will the color change?
  28.  *
  29.  ********************************************
  30.  * ENJOY!
  31.  * modified 10/17/02 Changed algorithms arround to make it render a little faster.
  32.  *
  33.  * NOTE .- This shader is VERY SLOW when you enable bumping, even SLOWER with
  34.  * displacements.
  35.  *
  36.  */
  37.  
  38.  
  39. #define repeat(x,freq)    (mod((x) * (freq), 1.0))
  40.  
  41. #define rotate2d(x,y,rad,ox,oy,rx,ry) \
  42.   rx = ((x) - (ox)) * cos(rad) - ((y) - (oy)) * sin(rad) + (ox); \
  43.   ry = ((x) - (ox)) * sin(rad) + ((y) - (oy)) * cos(rad) + (oy)
  44.  
  45.  
  46. #define fuzzpulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - \
  47.          smoothstep((b)-(fuzz),(b),(x)))
  48.  
  49. #define snoise(x)    (noise(x) * 2 - 1)
  50. #define snoise2(x,y) (noise(x,y) * 2 - 1)
  51. #define whichtile(x,freq) (floor((x) * (freq)))
  52. #define MINFILTERWIDTH  1e-7
  53. #define filterwidth_point(p) (max(sqrt(area(p)), MINFILTERWIDTH))
  54. #define udn(x,lo,hi) (smoothstep(.25, .75, noise(x)) * ((hi) - (lo)) + (lo))
  55.  
  56.  
  57. /* varyEach takes a computed color, then tweaks each indexed item
  58.  * separately to add some variation.  Hue, saturation, and lightness
  59.  * are all independently controlled.  Hue adds, but saturation and
  60.  * lightness multiply.
  61.  * Original by Larry Gritz. Modified to "hsv" by Rudy Cortes
  62.  */
  63. color varyEach (color Cin; float index, varyhue, varysat, varyval;)
  64. {
  65.     /* Convert to "hsv" space, it's more convenient */
  66.     color Chsv = ctransform ("hsv", Cin);
  67.     float h = comp(Chsv,0), s = comp(Chsv,1), v = comp(Chsv,2);
  68.     /* Modify Chsv by adding Cvary scaled by our separate h,s,v controls */
  69.     h += varyhue * (cellnoise(index+3)-0.5);
  70.     s *= 1 - varysat * (cellnoise(index-14)-0.5);
  71.     v *= 1 - varyval * (cellnoise(index+37)-0.5);
  72.     Chsv = color (mod(h,1), clamp(s,0,1), clamp(v,0,1));
  73.     /* Clamp hsl and transform back to rgb space */
  74.     return ctransform ("hsv", "rgb", clamp(Chsv,color 0, color 1));
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. surface k3d_stones (
  83.     float Ka = .6, Kd = .85,
  84.           Ks = 0, roughness = 1,
  85.           Km = 0.01,
  86.           displace = 0;
  87.     float minfreq = 1,
  88.           maxfreq = 10,
  89.           tilefreq = 6,
  90.           grungefreq = 30, grunge_Pow = 3, grunginess = - 0.8;
  91.    color  stonecolor = color(.7,.6,.4);
  92.    color  groundcolor = color (.45,0.35,0.2);
  93.    float varyhue = .03, varysat = .2, varylum = .25;)
  94.  
  95. {
  96.  color surface_color, layer_color;
  97.  float layer_opac;
  98.  float ss, tt, stile,ttile;
  99.  float freq,mag;
  100.  float r,theta,angle;
  101.  float d,d0,d1;
  102.  float cx,cy;
  103.  float noifreq = 10, noiscale = 0.3;
  104.  float bub;
  105.  vector V;
  106.  normal Nf;
  107.  float grunge;
  108.  
  109.  surface_color = groundcolor;
  110.  float surface_mag = 0;
  111.  
  112.  /*loop for creating color layers of rocks*/
  113.  for (freq = maxfreq ; freq>minfreq;freq -=0.5)
  114.   {
  115.    angle = PI * snoise(freq * 16.31456);  /*randomize angle index*/
  116.   
  117.    rotate2d(s,t,angle,0.5,0.5,cx,cy);    /*randomize rotations*/
  118.  
  119.    /*repeat tiles and find out in which tile we are at?*/
  120.    ss = repeat(cx,freq * tilefreq);
  121.    tt = repeat(cy,freq * tilefreq);
  122.    stile = whichtile(cx,freq * tilefreq);
  123.    ttile = whichtile(cy,freq * tilefreq);
  124.  
  125.    /*tile index to be use in vary each*/
  126.    float stoneindex = stile + 13 * ttile;
  127.  
  128.    /*create buble shapes*/
  129.    bub = 0.5 + snoise2(10 * freq,10 * freq);
  130.    ss += noiscale * snoise(snoise2(s * noifreq, t * noifreq) + 912);
  131.    tt += noiscale * snoise(snoise2(s * noifreq, t * noifreq) + 333);
  132.    /*cx = 0.5 + 0.1 * snoise2( 12.312,  21.773);
  133.    cy = 0.5 + 0.1 * snoise2( 28.398, 62.112); */
  134.    cx = 0.5 + 0.1 * snoise2( freq * 8.456, freq * 18.773);
  135.    cy = 0.5 + 0.1 * snoise2( freq * 28.398, freq * 42.112);
  136.    point p1 =(cx,cy,0);
  137.    point p2 = (ss,tt,0);
  138.    d = distance(p1, p2);
  139.    /*mag= ((0.5 * .8 - abs(bub - 0.5)) / .8) * 60 *(.09 - d * d) *
  140.               ((maxfreq - freq)/maxfreq); */
  141.    mag= (0.5 - abs(bub - 0.5)) * 90 *(.09 - d * d)*((maxfreq - freq)/maxfreq);
  142.  
  143.    layer_opac = clamp( mag,0,1);
  144.    /*create a diferent color for each rock*/
  145.    layer_color = varyEach(stonecolor, stoneindex,varyhue,varysat,varylum);
  146.    surface_color = mix(surface_color,layer_color,layer_opac);
  147.  
  148.  
  149.    /*calculate displacement if Km > .01*/
  150.      if(Km != 0)
  151.    {
  152.     surface_mag = max(surface_mag,mag);
  153.     }
  154. }
  155.  
  156.    /*apply if grunginess != 0 */
  157.    if(grunginess != 0)
  158.       {
  159.       /* compute turbulence  for grunge*/
  160.        point PP = transform("shader", P) * grungefreq;
  161.        float width = filterwidth_point(PP);
  162.        float cutoff = clamp(0.5 / width, 0, maxfreq);
  163.  
  164.         float turb = 0, f;
  165.         for (f = 1; f < 0.5 * cutoff; f *= 2)
  166.         turb += abs(snoise(PP * f)) / f;
  167.         float fade = clamp(2 * (cutoff - f) / cutoff, 0, 1);
  168.         turb += fade * abs(snoise(PP * f)) / f;
  169.  
  170.         grunge = pow(turb, grunge_Pow);
  171.         surface_mag += grunge * grunginess;
  172.         
  173.         }
  174.  
  175.   if (displace == 1)
  176.   {
  177.   P += Km * surface_mag * normalize(N);
  178.   N = normalize(calculatenormal(P)); 
  179.   }
  180.   else {
  181.   N = normalize(calculatenormal(P + Km * surface_mag * normalize(N)));
  182.   }
  183.  /*compute normals and vectors for shading*/
  184.   Nf = faceforward(normalize(N),I);
  185.   V = - normalize(I);
  186.  
  187. /*color output*/
  188.  
  189. Oi = Os;
  190. Ci = surface_color * Oi * (Ka * ambient() + Kd * diffuse(Nf))+
  191.       Ks * specular(Nf,V,roughness);
  192.  
  193. }
  194.  
  195.